home *** CD-ROM | disk | FTP | other *** search
/ MacPeople 2003 February 1 / MACPEOPLE-2003-02-01.ISO.7z / MACPEOPLE-2003-02-01.ISO / ぶらりオンラインウェアの旅 / 定番ソフト / Jedit(OS9) / MacroCollectionJ.sea / MacroCollection-J / レコードƒ / ート / スクリプト解説 < prev    next >
Text File  |  2001-01-02  |  6KB  |  149 lines

  1. レコードソートマクロ
  2. programmed by Satoshi Matsumoto <satoshi@matsumoto.co.jp>
  3. 1レコードが複数パラグラフにわたるデータのソートを行います。レコードの形式は正規表現でフレキシブルに指定できます。
  4.  
  5. ーーーーーーーーーーーーーーーーーーーーーー
  6.  
  7. ソート条件指定ダイアログ(Dialog Director用)のデータ
  8. property theSortDialog : {size:[210, 160], contents:[ツ
  9.     {class:push button, bounds:[140, 130, 200, 150], name:"OK", enabled:3}, ツ
  10.     {class:push button, bounds:[60, 130, 120, 150], name:"Cancel"}, ツ
  11.     {class:text field, bounds:[80, 10, 110, 10 + 16], name bounds:ツ
  12.         [10, 10, 75, 10 + 16], name:"キー位置:", value:"1"}, ツ
  13.     {class:pop up, name:"", bounds:[115, 8, 200, 10 + 16], name width:0, value:1, contents:ツ
  14.         "バイト目;文字目;ワード目"}, ツ
  15.     {class:check box, bounds:[10, 40, 200, 40 + 16], name:"降順", value:false}, ツ
  16.     {class:check box, bounds:[10, 60, 200, 60 + 16], name:"数値", value:false}, ツ
  17.     {class:check box, bounds:[10, 80, 200, 80 + 16], name:"大文字小文字の区別", value:true}, ツ
  18.     {class:check box, bounds:[10, 100, 200, 100 + 16], name:"全角半角の区別", value:true} ツ
  19.         ], timeout after:60}
  20.  
  21. レコード境界指定ダイアログ(Dialog Director用)のデータ
  22. property theRecordDialog : {size:[310, 100], contents:[ツ
  23.     {class:push button, bounds:[240, 70, 300, 90], name:"OK", enabled:3}, ツ
  24.     {class:push button, bounds:[160, 70, 220, 90], name:"Cancel"}, ツ
  25.     デフォルトのレコード境界は「空行"^¥r"で終わる」に指定してある
  26.     {class:text field, bounds:[10, 40, 210, 40 + 16], value:"^¥¥r"}, ツ
  27.     {class:pop up, bounds:[215, 38, 300, 40 + 16], value:2, contents:"で始まる;で終わる"}, ツ
  28.     {class:static text, bounds:[10, 10, 300, 10 + 16], contents:"レコードの特徴を正規表現で指定してください"} ツ
  29.         ], timeout after:60}
  30.  
  31. tell application "Jedit4"
  32.     Jedit4を前面へ
  33.     activate
  34.     Jeditのバージョンチェック。Rev4.0.4より古いときは中止
  35.     if version < 404 then
  36.         preDialog
  37.         display dialog "Jedit4.0 Rev4.0.4 以降を使用してください" buttons {" 了解"}
  38.         postDialog
  39.         error number -128
  40.     end if
  41.     書類がなにも開いていないときは警告をだして終了
  42.     if (count document) < 1 then
  43.         preDialog
  44.         display dialog "ブロックソートする書類を先に開いてください。" buttons {" 了解"}
  45.         postDialog
  46.         error number -128
  47.     end if
  48.     ソート条件設定ダイアログを表示
  49.     preDialog
  50.     set dialogResult to (dd auto dialog theSortDialog with grayscale)
  51.     postDialog
  52.     キャンセルボタンのときは中止
  53.     if item 1 of dialogResult is false then
  54.         error number -128
  55.     end if
  56.     ソート条件を各変数に格納する
  57.     set theKeyPosition to (item 3 of dialogResult) as integer
  58.     set theKeyType to item 4 of dialogResult
  59.     set flagDescending to item 5 of dialogResult
  60.     set flagNumeric to item 6 of dialogResult
  61.     set flagCase to item 7 of dialogResult
  62.     set flagZenkaku to item 8 of dialogResult
  63.     レコード境界指定ダイアログを表示
  64.     preDialog
  65.     set dialogResult to (dd auto dialog theRecordDialog with grayscale)
  66.     postDialog
  67.     キャンセルボタンのときは中止
  68.     if item 1 of dialogResult is false then
  69.         error number -128
  70.     end if
  71.     レコード境界指定条件を各変数に格納する
  72.     set thePattern to item 3 of dialogResult
  73.     set TopOrBottom to item 4 of dialogResult
  74.     tell document 1
  75.         if length of selection < 1 then
  76.             なにも領域が選択されていないときは、全書類を対象に
  77.             set startPara to 1
  78.             set endPara to paragraph number of last character
  79.         else
  80.             選択領域の開始・終了パラグラフ番号をstartPara・startPara変数に格納
  81.             set startPara to paragraph number of selection
  82.             set endPara to paragraph number of last character of selection
  83.         end if
  84.         ソート対象データをテキスト変数theTextへ格納
  85.         set theText to paragraphs startPara thru endPara
  86.     end tell
  87.     オリジナルの書類名をtheSourceDocに格納
  88.     set theSourceDoc to name of document 1
  89.     作業用の書類名を作成しtheTempDocに格納
  90.     set theTempDoc to theSourceDoc & ".tmp"
  91.     書類名theTempDocを新規作成
  92.     make new document
  93.     set name of document 1 to theTempDoc
  94.     テキスト変数theTex(ソート対象データ)を作業用ウィンドウへコピー
  95.     insert theText at end of document theTempDoc
  96.     tell document theTempDoc
  97.         カーソルを書類の先頭へ
  98.         set startOff to 1
  99.         set selection to before byte startOff
  100.         すべてのレコード境界文字列をrepeat loop で検索する
  101.         repeat while find thePattern in it with grep without wrap around
  102.             検索した文字列をtheStringへ格納
  103.             set theString to selection
  104.             if TopOrBottom = 1 then
  105.                 先頭レコード境界のときは、文字列"<RECORD>"をレコードの前に挿入する
  106.                 set theString to "<RECORD>" & theString
  107.             else
  108.                 終了レコード境界のときは、文字列"<RECORD>"をレコードの最後に挿入する
  109.                 set theString to theString & "<RECORD>"
  110.             end if
  111.             次に検索を開始する先頭オフセットnextOffを計算
  112.             set nextOff to byteoffset of selection
  113.             set nextOff to nextOff + (length of theString)
  114.             if nextOff < (count bytes) then
  115.                 書類の最後でなければ、みつかった選択領域をtheStringで置き換える
  116.                 set selection to theString
  117.                 カーソルを次の検索開始オフセットnextOffにセット
  118.                 set selection to before byte nextOff
  119.             end if
  120.         end repeat
  121.         全てのリターンコードを文字列"<CR>"に置き換える
  122.         replace return to "<CR>" in it with grep
  123.         レコード境界マーク"<RECORD>"をリターンコードに置き換える。これで、レコードが1パラグラフになる。
  124.         replace "<RECORD>" to return in it with grep
  125.         キータイプに応じて、jsortコマンドを実行
  126.         if theKeyType is 1 then
  127.             jsort it by byte_loc key position theKeyPosition descending flagDescending numeric flagNumeric case sensitive flagCase zenkaku sensitive flagZenkaku
  128.         else if theKeyType is 2 then
  129.             jsort character_loc key position theKeyPosition descending flagDescending numeric flagNumeric case sensitive flagCase zenkaku sensitive flagZenkaku
  130.         else
  131.             jsort word_loc key position theKeyPosition descending flagDescending numeric flagNumeric case sensitive flagCase zenkaku sensitive flagZenkaku
  132.         end if
  133.         ソートのために一時リターンコードを削除
  134.         replace return to "" in it with grep
  135.         文字列"<CR>"をもとのリータンコードに復元
  136.         replace "<CR>" to return in it with grep
  137.     end tell
  138.     ソート結果を文字変数theTextへコピー
  139.     set theText to document theTempDoc
  140.     作業用ウィンドウを閉じる
  141.     close document theTempDoc saving no
  142.     tell document theSourceDoc
  143.         対象データをソート結果と入れ替える
  144.         set paragraphs startPara thru endPara to theText
  145.         ソート結果を選択表示する
  146.         set selection to paragraphs startPara thru endPara
  147.     end tell
  148. end tell
  149.